<template>
	<view>
		<!-- 顶部导航栏 -->
		<detailsTabbar :vcolor="basic.color" :strategy_name="basic.strategy_name" :exchange_name="basic.exchange_name" :exchange_url="basic.exchange_url"></detailsTabbar>
		
		<!-- 中间内容区 -->
		<view v-if="content.show">
			<home v-if="bar.barPage === 0" :basic="basic" :vprogramstate="programState" v-on:selectPage="selectPage" v-on:setRobotStatus="getReloadRobotStatus"></home>
			<operation v-else-if="bar.barPage === 1" :basic="basic" :vprogramstate="programState" v-on:setProgramState="setProgramState"></operation>
			<config v-else-if="bar.barPage === 5" :basic="basic" v-on:barBasic="barBasic"></config>
			<monitor v-else-if="bar.barPage === 2" :basic="basic" v-on:selectPage="selectPage"></monitor>
			<mine v-else-if="bar.barPage === 3" :basic="basic" :vprogramstate="programState"></mine>
		</view>
		
		<!-- 底部导航栏 -->
		<bar v-if="bar.show" v-on:selectPage="selectPage" :vselected="bar.barPage" :vcolor="basic.color"></bar>
		
	</view>
</template>

<script>
	import detailsTabbar from '@/components/detailsTabbar/detailsTabbar.vue'
	import home from './components/home/home.vue'
	import operation from './components/operation/operation.vue'
	import config from './components/config/config.vue'
	import monitor from './components/monitor/monitor.vue'
	import mine from './components/mine/mine.vue'
	import bar from '@/components/bar/bar.vue'
	import { login, getRobotStatus } from '@/api/basic/basic.js'
	export default {
		components: {
			detailsTabbar, bar,
			home, operation, config, monitor, mine
		},
		data() {
			return {
				basic: {		// 页面基本参数
					color: '',
					exchange_domain: '',
					exchange_name: '',
					exchange_url: '',
					strategy_name: '',
				},
				programState: {	// 程序状态
					pan_opening: '1',	// 盘口类型  ('0', '实盘'), ('1', '模拟盘'),
					status: '-1',	// 程序状态 ('-1', '停止'), ('1', '运行中'), ('0', '平仓停止'),
					process: []	// 进程PID号
				},
				content: {		// 中间内容区变量
					show: false,	// 是否显示内容区
					robotStatus: '-1'	// 默认为停止状态
				},
				bar: {			// 底部导航栏变量
					show: false,	// 是否显示底部导航栏
					barPage: 0		// 默认页面
				}
				
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '加载中...',
			})
			this.basic = options
			console.log('页面基本参数', options)
			this.resetLogin()	// 进入页面时的登录操作
			setTimeout(function () {
				uni.hideLoading();
			}, 2000);
		},
		methods: {
			// 配置完成后的回调
			barBasic(resBasic) {
				console.log('配置完成后的回调', resBasic)
				this.bar.barPage = resBasic.pages
				this.content.show = true
				this.bar.show = true
				console.log('判断后端机器人程序是否启动.')
				this.robotStatus(
					resBasic.ipAddress,
					resBasic.exchangeDomain,
					resBasic.strategy,
				)
			},
			// 操作完之后的回调
			setProgramState(res) {
				this.programState.pan_opening = res.pan_opening
				this.programState.status = res.status
				this.programState.process = res.process
			},
			// 点击刷新之后的回调
			getReloadRobotStatus() {
				var _this = this
				uni.getStorage({
					key:_this.basic.exchange_domain + _this.basic.strategy_name,
					success(res) {
						console.log('获取Visit数据成功', res)
						_this.robotStatus(
							res.data.ip_address,
							_this.basic.exchange_domain,
							_this.basic.strategy_name,
						)
					},
					fail(err) {
						console.log('获取Visit数据失败', err)
						uni.showToast({ icon: 'none', title: `获取Visit数据失败: ${err.errMsg}` })
						setTimeout(function () {
							uni.reLaunch({
								url: '/pages/home/home'
							});
						}, 1000)
					}
				})
			},
			// 点击下方bar页面组件
			selectPage(resPages) {
				this.bar.barPage = resPages.pages
				console.log('点击页面', resPages.pages)
			},
			// 进入页面时的登录操作
			resetLogin() {
				let _this = this
				console.log('进入页面时的登录操作')
				uni.getStorage({
					key:'openId',
					success(openIdRes) {
						// 云数据库获取配置
						uniCloud.callFunction({
							name:'get_config',
							data:{
								open_id:openIdRes.data,
								exchange_domain:_this.basic.exchange_domain,
								strategy_name:_this.basic.strategy_name
							},
							success(callRes) {
								if (callRes.result.code === 200) {
									console.log('进入策略首页时config数据存在，获取后端数据', callRes.result)
									
									login(callRes.result.data.ip_address, {
										'username':callRes.result.data.username, 'password':callRes.result.data.password
									}).then(res =>{
										if (res.data.access) {
											console.log('access', res.data.access)
											console.log('refresh', res.data.refresh)
											res.data.ip_address = callRes.result.data.ip_address
											uni.setStorage({
												key:callRes.result.data.exchange_domain + callRes.result.data.strategy_name,
												data:res.data,
												success() {
													console.log('登录成功.')
													_this.robotStatus(
														callRes.result.data.ip_address,
														callRes.result.data.exchange_domain,
														callRes.result.data.strategy_name,
													)
													_this.content.show = true
													_this.bar.show = true
												},
												fail() {
													uni.showLoading({ title: 'token缓存失败' })
													setTimeout(function () {
														uni.reLaunch({
															url: '/pages/home/home'
														});
													}, 2000);
												}
											})
										} else {
											uni.showLoading({ title: '登录失败' })
											setTimeout(function () {
												uni.reLaunch({
													url: '/pages/home/home'
												});
											}, 2000);
										}
									}).catch(err => {
										console.log('登录的请求失败了, 捕获错误', err.errMsg)
										uni.showToast({ icon: 'none', title: `登录的请求失败了, 捕获错误: ${err.errMsg}` })
										setTimeout(function () {
											uni.reLaunch({
												url: '/pages/home/home'
											});
										}, 2000)
									})
									
								} else {
									console.log('进入策略首页时config数据不存在，页面应跳转到配置页面', callRes.result.msg)
									_this.content.show = true
									_this.bar.show = false
									_this.bar.barPage = 5
								}
							},
							fail(callErr) {
								console.log(`获取config失败: ${callErr.errMsg}`)
								uni.showToast({ icon: 'none', title: `获取config失败: ${callErr.errMsg}` })
							}
						})
						
					},
					fail(openIdErr) {
						console.log(`进入页面前获取openId缓存失败: ${openIdErr.errMsg}`)
						uni.showToast({ icon: 'none', title: `进入页面前获取openId缓存失败: ${openIdErr.errMsg}` })
						setTimeout(function () {
							uni.reLaunch({
								url: '/pages/login/login'
							});
						}, 2000);
					}
				})
			},
			// 获取机器人数据，判断是否已启动
			robotStatus(ipAddress, exchangeDomain, strategy) {
				let _this = this
				uni.getStorage({
					key:exchangeDomain + strategy,
					success(storageRes) {
						console.log('获取机器人数据，判断是否已启动', storageRes.data.access)
						getRobotStatus(ipAddress, storageRes.data.access, {
							exchange_domain: exchangeDomain, strategy_name: strategy
						}).then(res => {
							console.log('获取机器人数据，判断是否已启动成功', res)
							_this.programState.status = res.data.status
							_this.programState.pan_opening = res.data.pan_opening
							_this.programState.process = res.data.process
						}).catch(err => {
							console.log('获取机器人数据，判断是否已启动的请求失败了, 捕获错误', err)
							uni.showToast({ icon: 'none', title: `获取机器人数据，判断是否已启动的请求失败了, 捕获错误: ${err.errMsg}` })
							setTimeout(function () {
								uni.reLaunch({
									url: '/pages/home/home'
								});
							}, 1000)
						})
					},
					fail(storageErr) {
						console.log('获取缓存token失败', storageErr.errMsg)
						uni.showToast({ icon: 'none', title: `获取缓存token失败: ${storageErr.errMsg}` })
						setTimeout(function () {
							uni.reLaunch({
								url: '/pages/home/home'
							});
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style>

</style>
